#!/usr/bin/perl
#index.cgi
use Data::Dumper;

require './diskman-lib.pl';

&ReadParse();
$config{'mkfslock'} = "/var/nas/lock/mkfsd_new";
if( -e $config{'mkfslock'}) {
#   &redirect("volstat.cgi");
}
$out1 = `/sbin/MegaCli -pdlist -a0 > /etc/diskindex`;
$out2 = `/sbin/MegaCli -ldpdinfo -a0 > /etc/ldpdinfo`;
$out3 = `/sbin/MegaCli -ldinfo -lall -a0 > /etc/ldinfo`;
$out4 = `/sbin/MegaCli -cfgdsply -a0 > /etc/megacfg`; #lsicfg`;
&header($text{'diskman_title'}, "", "index", 0, 1, undef, undef, "<meta http-equiv=refresh content=100>");

my $query_text = mksysvol() ? 
			$text{'disk_format_query2'} :
			$text{'disk_format_query'};

my (@rdinfo, $r);
my (@dinfo, $d);
my ($mkfs_lock);
$mkfs_lock = check_lock();

@rdinfo = get_mega_raid_info();
@dinfo = get_all_lsi_diskinfo();
#print "<pre>",Dumper(@dinfo),"</pre>";
my ($validrd);
$validrd=0;
foreach $r (@rdinfo) {
	next if ($r->{'status'} eq 'null');
	$validrd++;
}
#@aabb=get_free_volnames(2);
#print @aabb;
#onsubmit_form(vcreate_exist, vdetail_exist, vrecovery_exist, vdelete_exist)
if ($validrd && has_free_disk(\@dinfo)) {
    #has volume and has free disk
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(1,1,1,1);'>\n";
}
elsif ($validrd) {
    #has volume, but hasn't free disk, cannot create volume
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(0,1,1,1);'>\n";
}
elsif (has_free_disk(\@dinfo)) {
    #has free disk, but hasn't volume, cannot detail, recovery, delete volume
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(1,0,0,0);'>\n";
}
else {
    #has no volume or free disk
    print "<form name=editform action=disk_man.cgi method=post onsubmit='return onsubmit_form(0,0,0,0);'>\n";
}

print "<table width=100% class=mode2><tr>\n";

print "<td  valign=top><table  class=mode1 border=0 cellspacing=0
	cellpadding=1 width=100%>\n";

print "<tr class=title1> <th align=left> $text{'index_dname'}</th>",
	"<th align=left> $text{'index_dstatus'}</th>",
	"<th align=left> $text{'index_dsize'}(MB)</th>",
	"<th align=left> $text{'index_duse'}</th>",
	"<th align=left> $text{'index_dmodel'}</th> </tr>\n",

my ($dsizeM, $i, $j,$found);
$i = 0;
$freemark = 0;
foreach $d (@dinfo) {
#	if(($d->{'id'}){
#		next;
#	}
	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}

	if ( $d->{'status'} eq 'SPARE' ) {
		$disable= ' ';
	}
	else {
		$disable= ' ';
		$found=1;
	}

	print "<tr $dismode>\n";
	print "<td nowarp $dismod width=20%>";
        $dvolname = get_volname($d->{'vdid'});
        $dvolname = "aa";
	$onclick="onclick=\'checkstatus(\"$d->{'status'}\", \"$dvolname\")\'";
        print "<input type=radio name=disk value=$d->{'id'} ";
	print " $disable $onclick >";
        if($d->{'id'} =~ /^(\d+)/) {
#	   $ppp = $1;
#	   if($ppp < 4){
#            	$tempdisk = "Disk".($ppp+1);
#	   }else{
#		$ll = $ppp-3;
		$tempdisk = "Disk$i" ;
#	   }
        }
        print $tempdisk; #print "|$d->{'id'}";
	print  "</td>\n";
	print "<td nowarp $dismod width=20%>";
	#print "$d->{'status'}";
        if($d->{'status'} eq 'Online'){
		print $text{'lsi_donline'};
        }
        elsif($d->{'status'} eq 'Hotspare'){
 		print $text{'lsi_dok'};
	}
	elsif($d->{'status'} eq 'null'){
		print "<i>", $text{'NA'}, "</i>";
	}
	elsif($d->{'status'} eq 'FAILED'){
		print $text{'lsi_dfailed'};
	}
	elsif($d->{'status'} =~ /good/){
		print $text{'lsi_dok'};
	}
        elsif($d->{'status'} eq 'Rebuild'){
		$rbld_prog = `/sbin/MegaCli -PDRbld -ShowProg -PhysDrv[$d->{'es'}] -a0`;
		
		print $text{'lsi_drbld'};
		if ($rbld_prog =~ /(\d+%)/) {
			print "(".$1.")";
		}
	}
        else{
		print $d->{'status'}; 
        }
	print "</td>\n";
	if ($d->{'capacity'} ne 'null') {
		$dsizeM=int($d->{'blocks'}/1024);
		print "<td nowarp $dismod width=20%>", $d->{'capacity'}, "</td>\n";
    if($d->{'raidid'} =~ /^\d+$/) {
          $tempvol = get_volname($d->{'vdid'}); #print "$d->{'raidid'}".$tempvol;
    }
    if ($d->{'raidid'} =~ /^null/) {
    	    $freemark = 1;
       	    print "<td nowarp $dismod width=20%>", $text{'unused'},
       	   "</td>\n";
    }
    elsif ($d->{'raidid'} =~ /SPARE/) {
          print "<td nowarp $dismod width=20%>$text{'lsi_dspare'}</td>\n";
    }
		else {
			print "<td nowarp $dismod width=20%>",
			&text('used', $tempvol), "</td>\n";
		}
	}
	else {
		print "<td nowarp $dismod width=20%>",
			"<i>", $text{'NA'}, "</i>", "</td>\n";
		print "<td nowarp $dismod width=20%>",
			"<i>", $text{'NA'}, "</i>", "</td>\n";
	}
	if ($d->{'style'} =~ /null/) {
		print "<td nowarp $dismod width=20%>",
		"<i>", $text{'NA'}, "</i>", "</td>\n";
	}
	else {
		print "<td nowarp $dismod width=20%>",
		$d->{'style'}, "</td>\n";
	}
	print "</tr>\n";
}

#print "<tr> <td colspan=5> <hr> </td> \n";

print "</td></tr></table>\n";


#print (format disk) button
$disable='disabled=false';
print "<td valign=top width=60>";
print "<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
print "<tr><td align=center><input type=submit name=dformat value=\"$text{'rescan'}\" onclick='return SetEditValue_disk(this,$i)' class=button ></td></tr>\n";
#if($hwinfo{'hotswap_support'}==1) {
$hotswap_support = 0;
if($hotswap_support) {
print "<tr><td align=center><input type=submit name=dhtswap value=\"$text{'dhtswap'}\" onclick='return SetEditValue_disk(this,$i)' class=button $disable ></td></tr>\n";
} else {
}
print "<tr><td align=center><input type=submit name=dspare value=\"$text{'sparedisk'}\" onclick='return SetEditValue_disk(this, $i)' class=button disabled=true></td></tr>\n";

print "</table></td>\n";
print "<tr> <td colspan=5> <hr> </td> \n";


#show the user volume information
print "<table width=100% class=mode2 cellspacing=0 cellpadding=0>\n";

print "<tr> <td  valign=top><table  class=mode1 border=0 cellspacing=0
cellpadding=1 width=100%>\n";
print "<tr class=title1> <th align=left> $text{'index_vname'}</th>",
	"<th align=left> $text{'index_vstatus'}</th>",
	"<th align=left> $text{'index_vrlevel'}</th>",
	"<th align=left> $text{'index_vpstatus'}</th>",
	"<th align=left> $text{'index_vintegrity'}</th>",
	"<th align=left> $text{'index_vcdisk'}</th> </tr>\n";
print "</tr>\n";

my ($has_usrvol);
$has_usrvol = 0;
$i = 0; $j = 0;
foreach $r (@rdinfo) {
	next if (($r->{'status'} eq 'none') || ($r->{'volname'} eq 'spare'));
	next if (!$r->{'volname'});
	$i++;
	if ($i % 2 == 0) {
		$dismode = "class=mode2";
	}
	else {
		$dismode = "class=mode1";
	}


	print "<tr $dismode>\n";
        if ($r->{'name'} =~ /^(\d+)/){
            $temp_volname = $r->{'volname'};
        }
	if ($r->{'status'} eq 'ormal' && $r->{'status'} eq 'building') {
		#an 'unknown usrvol
                print "unknown raid";
		print "<td $dismod width=13%>"; 
		print "<input type=radio name=volume value=$r->{'name'} >", $r->{'volname'}, "</td>\n";
		print "<td nowarp $dismod width=13%>", $text{'NA'}, "</td>\n"; #NA vstatus
		print "<td nowarp $dismod width=13%>", $text{'NA'}, "</td>\n"; #NA level
		print "<td nowarp $dismod width=17%>", $text{'NA'}, "</td>\n"; #NA pstatus
		print "<td nowarp $dismod width=17%>", $text{'NA'}, "</td>\n"; #NA integrity
		print "<td nowarp $dismod width=27%>", $text{'NA'}, "</td>\n"; #NA vcdisk
		print "</tr>\n";
		next;
	}

	#if the status is new or the raid is not integreted (has free disk), can edit this volume
	my ($can_recovery);
	#$can_recovery = can_recovery($r, \@dinfo);
        $can_recovery = new_can_recovery($r);
	if($can_recovery) {
		$can_recovery = $freemark;
	}
	print "<td $dismod width=13%>"; #print "$r->{'vdid'}|$r->{'name'}";
	if ($mkfs_lock) {
		print "<input type=radio name=volume value=$r->{'volname'} >", $r->{'volname'}, "</td>\n";
	}
	else {
		print "<input type=radio name=volume value=$r->{'vdid'} onclick='SetRecoveryStatus($can_recovery)' >", $temp_volname, "</td>\n";
	}

	print "<td nowarp $dismod width=13%>";
	if ($r->{'status'} eq 'Optimal') {
		print $text{'normal'};
	}
  elsif($r->{'status'} eq 'Degraded'){
          print $text{'degraded'};
  }
  elsif($r->{'status'} eq 'OFFLINE'){
          print $text{'lsi_ldoffline'};
  }
	else {
		print $text{'building'};
	}
	print "</td>\n";

	print "<td nowarp $dismod width=13%>RAID",
		$r->{'raidlevel'}, "</td>\n";
        $disks = get_unit_disks($r->{'name'});
	print "<td nowarp $dismod width=17%>";
#	if ($r->{'resync'}) {
#		print $text{'resync'};
#		if ($r->{'resync_delayed'}) {
#			print "(", $text{'delayed'}, ")";
#		}
#		else {
#			print "(", $r->{'percent'}, ")";
#		}
#	}
	if ($disks =~ /B/) {
		print $text{'recovery'};
#			"(", $r->{'percent'}."%", ")";
	}
	else {
		print $text{'normal'};
	}
	print "</td>\n";
	print "<td nowarp $dismod width=17%>";
#	if ($r->{'raid_disks'} != $r->{'working_disks'}) {
	if ($r->{'status'} =~ /Degraded/) {
		print $text{'unintegrated'};
	}
	else {
		print $text{'integrated'};
	}
	print "</td>\n";

	print "<td nowarp $dismod width=27%>";		
	my $k = 0;
	my($rdev, @rdisknames, $rdev_status, @cdinfo);
	for ($k = 0; $k < @{$r->{'rdevs'}}; $k++) {
		$rdev = $r->{'rdevs'}->[$k];
		$rdev_status = $r->{'rdevs_role'}->[$k];
		if ($rdev_status eq 'good') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(G)";
		}
		elsif ($rdev_status eq 'failed') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(F)";
		}
		elsif ($rdev_status eq 'spare') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(S)";
		}
		elsif ($rdev_status eq 'removed') {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(R)";
		}
		else {
			$cdinfo[$k] = &rdev2disk(\@dinfo,$rdev)."(U)";
		}
	}
       
	#print join("," , sort(@cdinfo));
#        $disks = get_unit_disks($r->{'name'});
#        print join("," , sort(@disks)); 
        #print $r->{'devs'};
        print $disks;
	print "</td>\n";

	print "</tr>\n";
}
print "</table> </td>\n";

#if has usr vol, then print (detail, recovery, edit, delete) button
if ($validrd) {
	print "<td valign=top width=60>",
    	"<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
	if ($mkfs_lock) {
	#print "<tr><td align=center><input type=submit name=vdetail value=\"$text{'detail'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vrecovery disabled=true value=\"$text{'vrecovery'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
        #print "<tr><td align=center><input type=submit name=filesystem value=\"FileSystem\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vdelete disabled=true value=\"$text{'delete'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	}
	else {
	#print "<tr><td align=center><input type=submit name=vdetail value=\"$text{'detail'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
        #print "<tr><td align=center><input type=submit name=filesystem value=\"FileSystem\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vrecovery disabled=true value=\"$text{'vrecovery'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	print "<tr><td align=center><input type=submit name=vdelete value=\"$text{'delete'}\" onclick='return SetEditValue(this,$i)' class=button></td></tr>\n";
	}
	print "</table></td>\n";
        #print "<tr><td><i> $text{'index_disk_explain2'} </i></td></tr>\n\n";
        print "<tr height=5><td> </td></tr>\n";
	print "<tr> <td><i>  $text{'index_disk_explain'} </i> </td> </tr>\n";
}


print "</tr>\n";

#create disk region
print "<tr height=20> <td> </td> </tr>\n";
if ($freemark) {   #if (new_has_free_disk()) {
	if ($mkfs_lock) {
		print "<tr><td align=left><input type=submit name=vcreate disabled=true value=\"$text{'create_vol'}\" onclick='return SetEditValue(this,$j)' class=button></td></tr>\n";
	}
	else {
		print "<tr><td align=left><input type=submit name=vcreate value=\"$text{'create_vol'}\" onclick='return SetEditValue(this,$j)' class=button></td></tr>\n";
	}

}
else {
	print "<tr><td> <i> $text{'nofreedisk'} </i></td></tr>\n";
}

if ($mkfs_lock == 1) {
	print "<tr height=20> <td> </td> </tr>\n";
	print "<tr><td> <i> $text{'fscklock'} </i></td></tr>\n";
}
elsif ($mkfs_lock == 2) {
	print "<tr height=20> <td> </td> </tr>\n";
	print "<tr><td> <i> $text{'noopdisk'} </i></td></tr>\n";
}

print "</table> </td>\n";

#-----------------logical volume-----------------#


#show the user volume information
print "<td valign=top width=60>",
"<table class=mode1 border=0 cellspacing=8 cellpadding=1 width=6%>\n";
#print "<tr><td align=center><input type=submit name=lvdetail value=\"$text{'detail'}\" class=button></td></tr>\n";
print "<tr><td align=center><input type=hidden name=lvformat value=\"$text{'format'}\" onclick='return SetEditValueLv(this,$ii)' class=button disabled=true></td></tr>\n";
print "</table></td>\n";
print "</table>\n";

print "<input type=hidden name=diskindex>\n";
print "<input type=hidden name=volname>\n";
print "<input type=hidden name=lvname>\n";
#java script
print <<EOF;
<script>
function onsubmit_form(vcreate_exist, vdetail_exist, vrecovery_exist, vdelete_exist)
{
        if (vcreate_exist == 1) {
	    document.editform.vcreate.disabled = true;
        }
        
        if (vdetail_exist == 1) {
            document.editform.vdetail.disabled = true;
        }
        
        if (vrecovery_exist == 1) {
            document.editform.vrecovery.disabled = true;
        }
        
        if (vdelete_exist == 1) {
            document.editform.vdelete.disabled = true;
        }
        document.editform.dformat.disabled = true;
}

function GetSelected_volume(obj, n)
{
	var i;
	var found = false;
	if (n > 1) {
		for (i = 0; i < document.editform.volume.length; i++) {
			if (document.editform.volume[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.editform.volume.checked) { 
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.editform.volname.value = document.editform.volume[i].value;
		else
			document.editform.volname.value = document.editform.volume.value;
		
	}
	else{
		document.editform.volname.value = '';
	}

	if (obj == document.editform.vcreate)
		found = true;

	return found;
}

function SetEditValue(obj, n)
{
	if (GetSelected_volume(obj, n))	{
		if (obj == document.editform.vdelete) {
			if (!confirm("$text{'delete_volume_query'}"))
				return false;
		} 
	}
	else {  
		alert("$text{'noselectvol'}");
		return false;
	}
	if (obj == document.editform.vcreate) {
		document.editform.action = 'vol_create.cgi';
	}
	else if (obj == document.editform.vdelete) {
		document.editform.action = 'mega_vol_delete.cgi';
	}
	else if (obj == document.editform.vrecovery) {
		document.editform.action = 'vol_recovery.cgi';
	}
        else if (obj == document.editform.filesystem) {
                document.editform.action = 'vol_detail.cgi';
        }
	else {
		document.editform.action = 'vol_detail.cgi';
	}

	return true;
}

function GetSelected_lv(obj, n)
{
        var i;
        var found = false;

        if (n > 1) {
                for (i = 0; i < document.editform.logivol.length; i++) {
                        if (document.editform.logivol[i].checked) {
                                found = true;
                                break;
                        }
                }
        }
        else if (n == 1) {
                if (document.editform.logivol.checked) {
                        found = true;
                }
        }
        if (found) {
                if (n > 1)
                        document.editform.lvname.value = document.editform.logivol[i].value;
                else
                        document.editform.lvname.value = document.editform.logivol.value;
        }
        else
                document.editform.lvname.value = '';

        return found;
}

function SetEditValueLv(obj, n)
{
        if (GetSelected_lv(obj, n)) {
                if (obj == document.editform.lvformat) {
                        if (!confirm("$text{'format_logivol'}"))
                                return false;
                }
        }
        else {
                alert("$text{'noselectlv'}");
                return false;
        }
        if (obj == document.editform.lvformat) {
                document.editform.action = 'lv_format.cgi';
        }
        return true;
}


function SetRecoveryStatus(CanRecovery)
{
	if (CanRecovery == 1) {
		document.editform.vrecovery.disabled = false;
	}
	else
	{
		document.editform.vrecovery.disabled = true;
	}
}
function checklvstatus(status){

	if (status == '1'){
		document.editform.lvformat.disabled = true;
	}
	if (status == '0'){
		document.editform.lvformat.disabled = false;
	}
}
function checkstatus(status, dvolname)
{
	//alert(status);
	if (status == 'Hotspare') {
		document.editform.dformat.disabled  = false;
//		document.editform.dhtswap.disabled  = true;
                document.editform.dspare.disabled = false;
	} 
	else if (status == 'Online') {
		document.editform.dformat.disabled  = false;
//		document.editform.dhtswap.disabled  = false;
                document.editform.dspare.disabled = true;
	}
	else if (status == 'offline') {
		document.editform.dformat.disabled  = false;
//		document.editform.dhtswap.disabled  = false;
	}
	else if (status == 'failed') {
		document.editform.dformat.disabled  = false;
//		document.editform.dhtswap.disabled  = true;
	}
        else if ((status == 'good') || (status == 'DEGRADED')){
                document.editform.dformat.disabled  = false;
//               document.editform.dhtswap.disabled  = true;
             if (dvolname == ''){
                document.editform.dspare.disabled = false;
             }
             else if(dvolname == 'spare') {
                document.editform.dspare.disabled = false;
             }
             else {
                document.editform.dspare.disabled = false;
             }
        }
        else if (status == 'null') {
                document.editform.dformat.disabled  = false;
//                document.editform.dhtswap.disabled  = true;
                document.editform.dspare.disabled = true;
        }
}

function SetEditValue_disk(obj, n)
{
	if (GetSelected_disk(obj, n))	{

                if (obj == document.editform.dhtswap) {
                        if (!confirm("Are you sure removing this disk?"))
                                return fals ;
                }
	}
	else {
                if(obj != document.editform.dformat){
		    alert("$text{'noselectdisk'}");
		    return false;
                }
	}
	if (obj == document.editform.dformat) {
		document.editform.action = 'diskrescan.cgi';
	}
	else if (obj == document.editform.dhtswap) {
//		document.editform.action = 'diskswap.cgi';
                document.editform.action = '3w_diskswap.cgi';
	}
        else if (obj == document.editform.dspare) {
                document.editform.action = 'spare.cgi';
        }
	return true;
}

function GetSelected_disk(obj, n)
{
	var i;
	var found = false;

	if (n > 1) {
		for (i = 0; i < document.editform.disk.length; i++) {
			if (document.editform.disk[i].checked) {
				found = true;
				break;
			}
		}
	}
	else if (n == 1) {
		if (document.editform.disk.checked) {
			found = true;
		}
	}

	if (found) {
		if (n > 1)
			document.editform.disk.value = document.editform.disk[i].value;
		else
			document.editform.disk.value = document.editform.disk.value;
	}
	else
		document.editform.disk.value = '';

	return found;
}

</script>

EOF

print "</form>\n";
&footer("/", $text{'index'});

